﻿using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.Table
{
    [XmlRoot("customFilters", Namespace = Constants.ExcelMain)]
    public class CustomFilters : XmlSerializable
    {
        [XmlAttribute("and", Form = XmlSchemaForm.Qualified)]
        public bool And { get; set; }

        [XmlElement("customFilter", Namespace = Constants.ExcelMain)]
        public XmlList<CustomFilter> CustomFilterList { get; set; }

        public CustomFilters()
        {
            And = false;
            CustomFilterList = new XmlList<CustomFilter>();
            CustomFilterList.Changing +=CustomFilterList_Changing;
        }

        void CustomFilterList_Changing(object sender, ListEventArgs<CustomFilter> args)
        {
 	        if(CustomFilterList.Count == 2 && 
                (args.Type == ListEventType.Add || args.Type == ListEventType.Insert))
            {
                args.Canceled = true;
            }
        }
    }
}
